<template>
	<!-- 我的采购 -->
	<!-- 顶部查询 -->
	<view class="content">
		<el-form ref="form" :model="form" label-width="80px" class="orderStatus">
			<el-form-item label="商品名称:" class="order">
				<el-input v-model="form.Article_Name" style="width: 188px" placeholder="请输入商品名称"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="handleclick()">发布</el-button>
				<el-button type="success">查询</el-button>
				<el-button @click="Black" type="danger">返回</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="tableData" border :header-cell-style="{ 'text-align': 'center' }"
			:cell-style="{ 'text-align': 'center' }" style="width: 97%;">
			<el-table-column type="index" label="序号" width="55"></el-table-column>
			<el-table-column label="商品名称" width="85" align="center">
				<template v-slot="{row}">
					<el-tooltip effect="dark" :content="row.Article_Name" placement="top">
						<view class="text-ellipsis w-100">
							<text>{{row.Article_Name}}</text>
						</view>
					 </el-tooltip>
				</template>
			</el-table-column>
			<el-table-column prop="Quantity" label="交易量" width="85">
				<template v-slot="{row}">
					{{row.Quantity}}{{row.Unit}}
				</template>
			</el-table-column>
			<el-table-column prop="Amount" label="交易金额" width="85">
				<template v-slot="{row}">
					{{Number(row.Amount)*Number(row.Quantity)}}元
				</template>
			</el-table-column>
			<el-table-column prop="Collection_Amount" label="已付金额" width="85">
				<template v-slot="{row}">
					{{row.Collection_Amount}}元
				</template>
			</el-table-column>
			<el-table-column prop="Collection_Amount" label="未付金额" width="85">
				<template v-slot="{row}">
					{{(Number(row.Amount)*Number(row.Quantity)-Number(row.Collection_Amount)).toFixed(2)}}
				</template>
			</el-table-column>
			<el-table-column label="联系人">
				<template v-slot="{row}">
					<el-tooltip effect="dark" :content="row.Seller_Contact" placement="top">
						<view class="text-ellipsis w-100">
							<text>{{row.Seller_Contact}}</text>
						</view>
					 </el-tooltip>
				</template>
			</el-table-column>
			<el-table-column :show-overflow-tooltip="true" effect="dark" prop="Enterprise_Name" label="对方企业"
				width="150"></el-table-column>
			<el-table-column label="交易状态" width="85">
				<template v-slot="{row}">
					<text v-if="row.Row_Status=='0'&&row.Seller_Status=='0'">待我方审核</text>
					<text v-if="row.Row_Status=='2'&&row.Buyer_Status=='0'">我方已拒绝申请</text>
					<text v-if="row.Row_Status=='1'&&row.Seller_Status=='0'">待双方确认合同</text>
					<text v-if="row.Row_Status=='4'&&row.Seller_Status=='0'">待对方确认合同</text>
					<view v-if="row.Trading_Name!='全款'" class="">
						<text v-if="row.Row_Status=='4'&&row.Seller_Status=='4'&&row.Trading_Detail_SerialNumber.includes('T01000000000001_0')">待我方付首款</text>
						<text v-if="row.Row_Status=='4'&&row.Seller_Status=='4'&&row.Trading_Detail_SerialNumber.includes('T01000000000001_1')&&row.Trading_Detail_SerialNumber.includes('T01000000000002_0')">待对方发货</text>
						<text v-if="row.Row_Status=='5'&&row.Seller_Status=='4'&&row.Trading_Detail_SerialNumber.includes('T01000000000002_0')">待我方收货</text>
						<text v-if="row.Row_Status=='6'&&row.Seller_Status=='4'&&row.Trading_Detail_SerialNumber.includes('T01000000000001_0')">待我方付首款</text>
						
						<text v-if="row.Row_Status=='6'&&row.Seller_Status=='4'&&row.Trading_Detail_SerialNumber.includes('T01000000000001_1')&&row.Trading_Detail_SerialNumber.includes('T01000000000002_0')">待我方付下一笔款</text>
						<text v-if="row.Row_Status=='6'&&row.Seller_Status=='4'&&row.Trading_Detail_SerialNumber.includes('T01000000000002_1')&&row.Trading_Detail_SerialNumber.includes('T01000000000003_0')">待我方付下一笔款</text>
						<text v-if="row.Row_Status=='6'&&row.Seller_Status=='4'&&row.Trading_Detail_SerialNumber.includes('T01000000000003_1')&&row.Trading_Detail_SerialNumber.includes('T01000000000004_0')">待我方付尾款</text>
					</view>
					<view v-if="row.Trading_Name=='全款'" class="">
						<text v-if="row.Seller_Status=='4'&&row.Row_Status=='4'&&row.Trading_Detail_SerialNumber.includes('T01000000000001_0')">待我方付全款</text>
						<text v-if="row.Seller_Status=='4'&&row.Row_Status=='4'&&row.Trading_Detail_SerialNumber.includes('T01000000000002_0')">待我方付全款</text>
						<text v-if="row.Seller_Status=='4'&&row.Row_Status=='4'&&row.Trading_Detail_SerialNumber.includes('T01000000000003_0')">待我方付全款</text>
						<text v-if="row.Seller_Status=='4'&&row.Row_Status=='4'&&row.Trading_Detail_SerialNumber.includes('T01000000000004_0')">待我方付全款</text>
						<text v-if="row.Trading_Detail_SerialNumber.includes('T01000000000001_1')&&row.Seller_Status=='4'&&row.Row_Status=='4'">待对方发货</text>
						<text v-if="row.Trading_Detail_SerialNumber.includes('T01000000000002_1')&&row.Seller_Status=='4'&&row.Row_Status=='4'">待对方发货</text>
						<text v-if="row.Trading_Detail_SerialNumber.includes('T01000000000003_1')&&row.Seller_Status=='4'&&row.Row_Status=='4'">待对方发货</text>
						<text v-if="row.Trading_Detail_SerialNumber.includes('T01000000000004_1')&&row.Seller_Status=='4'&&row.Row_Status=='4'">待对方发货</text>
						<text v-if="row.Row_Status=='5'&&row.Seller_Status=='4'">待我方收货</text>
					</view>
					<text v-if="Number(row.Amount)*Number(row.Quantity)==Number(row.Collection_Amount)&&row.Row_Status=='6'&&row.Seller_Status
=='4'">交易结束</text>
				</template>
			</el-table-column>
			<el-table-column prop="operate" label="操作" width="150">
				<template v-slot="{row}">
					<view class="flex justify-center">
						<el-button :loading="loading" v-if="row.Row_Status == '0'" @click="Inventory(row)" size="mini"
							type="success">通过</el-button>
						<el-button :loading="loading" @click="Confirmcontract(row)" v-if="row.Row_Status == '1'" size="mini"
							type="success">确认合同</el-button>
						<el-button v-if="row.Row_Status=='4'&&row.Seller_Status=='4'||row.Row_Status=='6'&&row.Seller_Status=='4'" @click="goDetail(row)" size="mini" type="primary">付款详情</el-button>
						<el-button @click="RefuseProcurement(row)" :loading="loading" v-if="row.Row_Status == '0'"
							size="mini" type="danger">拒绝</el-button>
						<el-button :loading="loading" @click="ConfirmReceipt(row)" v-if="row.Row_Status == '5'" size="mini"
							type="warning">确认收货</el-button>
					</view>
				</template>
			</el-table-column>
		</el-table>
		<el-dialog title="" :visible.sync="dialogVisible" width="80%">
			<el-table :data="orderData" border style="width: 97%;">
				<el-table-column prop="number" label="总数量" width="70"></el-table-column>
				<el-table-column prop="acquisition" label="收购量" width="70"></el-table-column>
				<el-table-column prop="price" label="价格" width="55"></el-table-column>
				<el-table-column prop="enterprise" label="所属企业" width="85"></el-table-column>
				<el-table-column prop="contacts" label="联系人" width="70"></el-table-column>
				<el-table-column :show-overflow-tooltip="true" prop="contactsphone" label="联系人电话"
					width="110"></el-table-column>
				<el-table-column prop="package" label="包装方式" width="105"></el-table-column>
				<el-table-column prop="payment" label="支付方式" width="85"></el-table-column>
				<el-table-column prop="remit" label="付款方式" width="80"></el-table-column>
				<el-table-column :show-overflow-tooltip="true" effect="dark" prop="logistics" label="物流方式"
					width="85"></el-table-column>
				<el-table-column :show-overflow-tooltip="true" effect="dark" prop="state" label="状态"
					width="85"></el-table-column>
				<el-table-column :show-overflow-tooltip="true" effect="dark" prop="time" label="预约时间"
					width="85"></el-table-column>
			</el-table>
		</el-dialog>
		<view class="flex " style="justify-content: space-between;">
			<view class="">
				
			</view>
			<el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="form.page" :page-sizes="[1,5,10,20]" :page-size="form.limit"
				layout="total, sizes, prev, pager, next, jumper" :total="total">
			</el-pagination>
		</view>
	</view>

</template>

<script>
	const app = getApp()
	import times from '@/static/js/utils.js'
	import {
		Message
	} from 'element-ui'
	import {
		getGroupSeekTransactionAllAction
	} from '@/api/purchasedgoods.js'
	import {
		GroupSeekCheckStockAction,
		GroupSeekTransactionExamineAction,
		getGroupSeekProcureIDAction,
		GroupSeekTransactionStateAction
	} from '@/api/group.js'
	import {
		getSubtableInquiryOriginal,
		getEditAction
	} from '@/api/global.js'
	export default {
		name: "procure",
		data() {
			return {
				currentPage1: 5,
				currentPage2: 5,
				currentPage3: 5,
				currentPage4: 4,
				Group_Seek_Transaction_SerialNumber: '',
				Domain_IMG: app.globalData.Domain_IMG,
				dialogVisible: false,
				form: {
					Type: "1",
					Group_Seek_Purchase_SerialNumber: '',
					Enterprise_Info_SerialNumber: uni.getStorageSync('loginInfo').enterprise,
					page: 1, //页数
					limit: 10, //每页数据条数
				},
				loading: false,
				total: 0,
				tableData: [],
				orderData: [{
					number: '10',
					acquisition: '1',
					price: '1',
					enterprise: '西货郎',
					contacts: '123陈',
					contactsphone: '12345678911',
					package: '拼箱',
					payment: '按阶段付款',
					remit: '指定账户打款',
					logistics: '海洋运输',
					state: '5',
					time: '2023-12-15 09:50:37'
				}],
			};
		},
		mounted() {
			this.getList(this.form);
		},
		methods: {
			handleclick() {
				uni.navigateTo({
					url: '/pages/release/purchase-release'
				})
			},
			Black() {
				this.$emit('Black', 6)
			},
			handleCurrentChange(e) {
				this.form.page = e
				this.getList(this.form)
			},
			handleSizeChange(e) {
				this.form.page = 1
				this.form.limit = e
				this.getList(this.form)
			},
			async getList(data) {
				//分页查询所有采购数据
				const res = await getGroupSeekTransactionAllAction({
					data,
				})
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url:'/pages/login/login?expire=1'
					})
				} else if (res.Status == 'success') {
					console.log(res);
					this.tableData = res.tableData
					this.total = parseInt(res.limit)
				}
			},
			async CheckStock(data) {
				const res = await GroupSeekCheckStockAction({
					data,
				})
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url:'/pages/login/login?expire=1'
					})
				} else if (res.Status == 'success') {
					console.log(res);
					let data = {
						Group_Seek_Transaction_SerialNumber: this.Group_Seek_Transaction_SerialNumber,
						Audit_Status: '1',
						Cause: '',
						token: uni.getStorageSync('loginInfo').token
					}
					this.GroupSeekTransactionExamine(data,'已确认!')
				}
			},
			async GroupSeekTransactionExamine(data,message) {
				const res = await GroupSeekTransactionExamineAction({
					data,
				})
				this.loading = false
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url:'/pages/login/login?expire=1'
					})
				} else if (res.Status == 'success') {
					console.log(res);
					uni.hideLoading()
					Message({
						message,
						type: 'success'
					})
					this.getList(this.form)
				}
			},
			async getGroupSeekProcure(data) {
				const res = await getGroupSeekProcureIDAction({
					data,
				})
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url:'/pages/login/login?expire=1'
					})
				} else if (res.Status == 'success') {
					console.log(res);
					let obj = {
						// 采购信息表
						group_seek_transaction: {
							group_seek_transaction_paystages: {}
						}
					}
					let Data = {
						Primary_Key: this.Group_Seek_Transaction_SerialNumber,
						JSONObject: JSON.stringify(obj),
						token: uni.getStorageSync('loginInfo').token
					}
					this.getSubtableInquiry(Data)
				}
			},
			async getSubtableInquiry(data) {
				const res = await getSubtableInquiryOriginal({
					data,
				})
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url:'/pages/login/login?expire=1'
					})
				} else if (res.Status == 'success') {
					console.log(res)
					res.group_seek_transaction[0].Row_Status = '4'
					let group_seek_message = {
						Group_Seek_Message_SerialNumber: "",
						Group_Seek_Transaction_SerialNumber: res.group_seek_transaction[0]
							.Group_Seek_Transaction_SerialNumber,
						Cause: "确认合同",
						Submission_Time: times.nowtime(),
						Enterprise_Info_SerialNumber: uni.getStorageSync('loginInfo').enterprise,
						Seller_Type: "0"
					}
					let Data = {
						Data: JSON.stringify({
							Modify: {
								group_seek_transaction: [res.group_seek_transaction[0]]
							},
							Add: {
								group_seek_message: [group_seek_message]
							}
						}),
						token: uni.getStorageSync('loginInfo').token,
					}
					this.getEdit(Data)
				}
			},
			Inventory(row) {
				this.loading = true
				this.Group_Seek_Transaction_SerialNumber = row.Group_Seek_Transaction_SerialNumber
				uni.showLoading({
					title: '提交中...'
				})
				let data = {
					Group_Seek_Purchase_SerialNumber: row.Group_Seek_Purchase_SerialNumber,
					Quantity: row.Quantity,
					token: uni.getStorageSync('loginInfo').token
				}
				this.CheckStock(data)
			},
			Confirmcontract(row) {
				this.loading = true
				this.Group_Seek_Transaction_SerialNumber = row.Group_Seek_Transaction_SerialNumber
				uni.showLoading({
					title: '提交中...'
				})
				let data = {
					Group_Seek_Purchase_SerialNumber: row.Group_Seek_Purchase_SerialNumber,
					token: uni.getStorageSync('loginInfo').token
				}
				this.getGroupSeekProcure(data)
			},
			async getEdit(data) {
				const res = await getEditAction({
					data,
				})
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url:'/pages/login/login?expire=1'
					})
				} else {
					this.loading = false
					console.log(res)
					uni.hideLoading()
					Message({
						message: '已确认合同',
						type: 'success'
					})
					this.getList(this.form)
				}
			},
			ConfirmReceipt(row) {
				this.loading = true
				uni.showLoading({
					title: '提交中...'
				})
				let data = {
					Group_Seek_Transaction_SerialNumber: row.Group_Seek_Transaction_SerialNumber,
					token: uni.getStorageSync('loginInfo').token
				}
				this.GroupSeekTransactionState(data)
			},
			async GroupSeekTransactionState(data) {
				const res = await GroupSeekTransactionStateAction({
					data,
				})
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url:'/pages/login/login?expire=1'
					})
				} else if (res.Status == 'success') {
					this.loading = false
					uni.hideLoading()
					Message({
						message: '已确认收货',
						type: 'success'
					})
					this.getList(this.form)
				}
			},
			RefuseProcurement(row) {
				this.loading = true
				uni.showLoading({
					title: '提交中...'
				})
				let data = {
					Group_Seek_Transaction_SerialNumber: row.Group_Seek_Transaction_SerialNumber,
					Audit_Status: '2',
					Cause: '拒绝申请',
					token:uni.getStorageSync('loginInfo').token
				}
				this.GroupSeekTransactionExamine(data,'已拒绝申请')
			},
			goDetail(row) {
				console.log(row)
				uni.setStorageSync('detail',JSON.stringify(row))
				uni.navigateTo({
					url: '/pages/order/purchase-order-detail?Group_Seek_Transaction_SerialNumber=' + row
						.Group_Seek_Transaction_SerialNumber + '&Group_Seek_Purchase_SerialNumber=' + row
						.Group_Seek_Purchase_SerialNumber+'&a=0'
				})
			}
		}
	};
</script>

<style>
	.content {
		width: 100%;
		padding: 30rpx;

		background-color: #fff;

		.el-form {
			display: flex;
			flex-wrap: wrap;
		}
	}
</style>